const mongoose = require('mongoose')
// 连接mongodb数据库
mongoose.connect('mongodb://127.0.0.1:27017/bilibili')
mongoose.connection.on('open', async () => {  //连接成功的回调
  console.log('连接成功')

  const carSchema = mongoose.Schema({
    brand:{
      type:String,
      require:true
    },
    VIN:{ //车架号
      type:String,
      unique:true //唯一，需要重建集合才生效
    },
    price:{
      type:Number,
      default:998
    },
    name:{
      type:String,
      enum:['a','b','c']
    }
  })

  const Car = mongoose.model('cars',carSchema)

  const result =  new Car({
    brand:'长安',
    VIN:'54654564',
    name:'b'
  })

  await result.save()


})
mongoose.connection.on('error', () => {  //连接失败的回调
  console.log('连接失败')
})
mongoose.connection.on('close', () => {  //连接关闭的回调
  console.log('连接关闭')
})